package com.livestocksolutions.intent;

import java.util.Date;

import com.livestocksolutions.R;
import com.livestocksolutions.ui.ViewObservationArrayAdapter;
import com.livestocksolutions.util.StringArrayParcelable;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;

/* -- Version History --
 *  1.0 : 22 July 2012 - Initial version.
 * 
 *  1.1 : 13 August 2012 - Created super class BaseActivity to hold all methods that are
 *                         standard across Activities
 *  1.2 : 08 September 2012 - Changed underlying structure to avoid using ListView for 
 *                         listed items that can be modifiable so that everything behaves
 *                         on screen rotation and upon the display of the virtual keyboard.
 */

/**
 * This class manages the display of an Observation Object
 * @author Adam Parr
 * @version 1.2
 * 
 * @.modified 16 September 2012
 */
public class ObservationActivity extends BaseActivity {
	public static final int OBSERVATIONVIEW 		 	= 0;
	/**
	 * Stores the reference to the XML layout template for each view
	 * @see #OBSERVATIONVIEW
	 */
	public static final Integer[] RESOURCEID = new Integer[]{
		R.layout.item
	};
	
	/**
	 * The value of the position in the array of the owning object.<br>
	 * A value of -1 indicates this is a new Item.
	 */
	private int id = -1;

	/**
	 * Observation View that holds the data of the Observation
	 */
	private ViewObservationArrayAdapter observationView;
	
	/**
	 * Constructor
	 */
	public ObservationActivity() {
		super(RESOURCEID);
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if(stack.size()==0){
        	setContentView(RESOURCEID[OBSERVATIONVIEW]);
    		if(!built){
    			// If Observation ever becomes viewable only remove the following line.
        		edit=true;
    			// Put the ID of this Observation Object
    			id = id!=-1?id:getIntent().getExtras().getInt(ITEMID,-1);
	    		// Read initial state of the Observation Object and add to observationValues
    			String[] observationValues;
    			
	    		if(id>=0){
		    		observationValues = getIntent().getExtras().getStringArray("data");
	    		}else{
	    			// A new Observation
	    			observationValues = new String[ViewObservationArrayAdapter.SIZE];
	    			observationValues[ViewObservationArrayAdapter.ID] = String.valueOf(-1);
	    			observationValues[ViewObservationArrayAdapter.DATE] = String.valueOf(new Date().getTime());
	    			observationValues[ViewObservationArrayAdapter.TYPE] = String.valueOf(ViewObservationArrayAdapter.TYPE_OBSERVATION);
	    			observationValues[ViewObservationArrayAdapter.VALUE] = "";
	    		}
	
	    		observationView = new ViewObservationArrayAdapter(this, observationValues);
	    		
	    		setPage();
	            
				currentState.putInt(STATEID,OBSERVATIONVIEW);
	    		built = true;
	    	}
    	}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	// Show the menu for an Observation Object
        getMenuInflater().inflate(R.menu.observation_menu, menu);
        this.menu=menu;
        return true;
    }
    
    @Override
    public void onSaveInstanceState (Bundle outState) {
    	super.onSaveInstanceState (outState);
    	
    	outState.putParcelable("observationView", new StringArrayParcelable().setStrings(observationView.get()));
    }
    
    @Override
    public void onRestoreInstanceState(Bundle inState) {
    	observationView = new ViewObservationArrayAdapter(this,((StringArrayParcelable)inState.getParcelable("observationView")).getStrings()[0]);

    	super.onRestoreInstanceState (inState);
    }
    
    @Override
    public boolean frameSelection(int frame) {
        switch (frame) {
        	case R.id.menu_save:
        		// Get the latest version of this Object set by the user
        		if(observationView.isValid()){
        			getIntent().putExtra(ITEMID,id);
	        		getIntent().putExtra("data", observationView.getItem(0));
	        		setResult(RESULT_OK, getIntent());
	        		finish();
        		}
        		break;
	        default:
	        	return super.frameSelection(frame);
        }
        return true;
    }
    
    @Override
	public boolean constructFrame(int view) {
    	switch (view) {
	        case OBSERVATIONVIEW:
	        	setContentView(RESOURCEID[OBSERVATIONVIEW]);
	        	setPage();
	            break;
	        default:
	            return super.constructFrame(view);
    	}
    	return true;
    }
    
    private void setPage(){
    	LinearLayout itemView = (LinearLayout) findViewById(R.id.listView);
    	// Not Implemented for an Observation
    	if(edit){
    		//observationView.setModifyObservation();
	    }else{
	    	//observationView.setViewObservation();
	    }
    	itemView.removeAllViews();
    	observationView.setRoot(itemView);
    	View v = observationView.getView(0,null, itemView);
    	itemView.addView(v,0);
    }
}